<?php

declare(strict_types=1);
/**
 * 创建人 : Forpastime
 *
 * @link     http://hypercmf.com
 * @document https://hyperf.wiki
 * @contact  forpastime@qq.com
 * @git  https://gitee.com/hypercmf/hypercmf1.0.git
 */
namespace App\Controller\Admin;

use App\Controller\AdminBaseController;
use App\Service\AdminService;
use App\Utils\Result;
use Hyperf\Contract\SessionInterface;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpMessage\Cookie\Cookie;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\Redis\Redis;
use Hyperf\Utils\ApplicationContext;
use Hyperf\Utils\Network;

/**
 * @Controller(prefix="admin/login")
 */
class LoginController extends AdminBaseController
{
    /**
     * @Inject()
     * @var AdminService
     */
    protected $adminService;

    /**
     * @Inject
     * @var SessionInterface
     */
    protected $session;

    /**
     * @Inject()
     * @var RequestInterface
     */
    protected $request;
    protected $redis;
    public function __construct()
    {
        $context = ApplicationContext::getContainer();
        $this->redis = $context->get(Redis::class);
    }

    /**
     * @GetMapping(path="login")
     * 后台登陆 token模式
     */
    public function login()
    {
        $param = $this->request->all();
        // 判断登陆信息
        if (empty($param['username'])) {
            return $this->error('用户名不能为空');
        }
        if (empty($param['password'])) {
            return $this->error('密码不能为空');
        }
        $res = $this->adminService->login($param);
        if($res['code']==-1){
            return Result::error($res['msg']);
        }
        if($res['code']==1){
            $this->adminService->editInfo(['id'=>$res['data']['id'],'last_login_time'=>time(),'last_login_ip'=>Network::ip()]);

            $token = for_random_string(32);
            $this->redis->setex('HyperCMF:admin_token:'.$param['username'],3600*24,$token);

            //存入redis，中间件直接查询使用
            $this->redis->set('HyperCMF:admin_role:'.$param['username'],$res['data']['role_id']);

            $data= $res['data'];
            $data['token'] = $token;
            return Result::success($data);
        }else{
            return Result::error($res['msg']);
        }
    }

    /**
     * @PostMapping(path="login2")
     * 后台登陆. cookie模式
     * @return \Psr\Http\Message\ResponseInterface|string|void
     */
    public function login2()
    {
        var_dump($this->request->getCookieParams('PHPSESSID'));
        //判断cookie和session是否一致
        if (!empty($this->session->get('PHPSESSID')) && $this->session->get('PHPSESSID')==$this->request->cookie('PHPSESSID')) {
            echo "login =";
            echo $this->session->get('PHPSESSID');
            return $this->response->redirect('admin/index/index');
        }

        $param = $this->request->all();

        if ($this->request->isMethod('post')) {
            echo "login !=".$this->session->get('PHPSESSID');
            // 判断登陆信息
            if (empty($param['username'])) {
                return $this->error('用户名不能为空');
            }
            if (empty($param['password'])) {
                return $this->error('密码不能为空');
            }
            if ($param['username'] == 'abc' && $param['password'] == '123') {
                $id = for_random_string(32);
                $this->session->set('PHPSESSID',$id);
                $cookie = new Cookie('PHPSESSID',$id);
                return $this->response->withCookie($cookie)->redirect('admin/index/index');
            }
        }
    }

    /**
     * @GetMapping(path="logout")
     */
    public function logout()
    {
        $res = $this->redis->del($this->request->query('username'));
        if($res){
            return Result::success([]);
        }else{
            return Result::error('退出登陆失败');

        }
    }
    /**
     * 退出登陆.
     * @return \Psr\Http\Message\ResponseInterface|string
     */
    public function logout1()
    {
        $this->session->remove('user');
        return $this->redirect('admin/login/login');
    }
}
